package com.example.farmeri.ui.mine;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import androidx.annotation.MainThread;
import androidx.appcompat.app.AlertDialog;

import com.example.farmeri.R;
import com.example.farmeri.data.config.ServiceConfig;
import com.example.farmeri.pojo.vo.EvenBusSuccess;
import com.example.farmeri.presenter.mine.AlertPwdPresenter;
import com.example.farmeri.presenter.mine.IAlertPwdContract;
import com.example.farmeri.tools.FormatUtils;
import com.example.farmeri.tools.OKHttpClientUtils;
import com.example.farmeri.ui.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改密码
 */
public class AlertPasswordActivity extends BaseActivity<AlertPwdPresenter> implements IAlertPwdContract {

    private AlertDialog dialog;

    @BindView(R.id.alert_pwd_showPwd)
    CheckBox showPwd;

    @BindView(R.id.alert_pwd_oldPwd)
    EditText oldPwd;
    @BindView(R.id.alert_pwd_newPwd)
    EditText newPwd;
    @BindView(R.id.alert_pwd_reNewPwd)
    EditText reNewPwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_password);
        ButterKnife.bind(this);
        initToolBar(R.id.alert_pwd_toolbar,"修改密码",true,true);
        //设置提示框
        initDialog();

        init();

        showPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    oldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    newPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    reNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    oldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    reNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }

    private void init() {
        present = new AlertPwdPresenter(this);
    }

    /**
     * 修改密码提交
     */
    @OnClick(R.id.alert_pwd_submit)
    public void submit(){
        String oldPwdText = oldPwd.getText().toString();
        String newPwdText = newPwd.getText().toString();
        String reNewPwdText = reNewPwd.getText().toString();
        if (FormatUtils.checkPassword(newPwdText)){
            showToast(this,"密码长度为8到20位,必须包含字母和数字，字母区分大小写");
        }
        if (!newPwdText.equals(reNewPwdText)){
            showToast(this,"新密码不匹配");
            return;
        }

        SharedPreferences sp = getSharedPreferences(getString(R.string.share_preference_user_info), Context.MODE_PRIVATE);
        String id = sp.getString(getString(R.string.user_info_key_id),"");
        if (id.equals("")){
            showToast(this,"当前用户不存在");
            return;
        }

        Map<String ,String> map = new HashMap<>();
        map.put("oldPwd",oldPwdText);
        map.put("newPwd",newPwdText);
        map.put("id",id);

        present.alertPwd(map);
//        OKHttpClientUtils.userPost(ServiceConfig.alertPwd(), OKHttpClientUtils.TYPE_FORM, map, new EvenBusSuccess());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onResult(String o){
        if ("success".equals(o.toString())){
            dialog.show();
        }else {
            showToast(o.toString());
        }
    }

    private void initDialog() {
        dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("提示");
        dialog.setMessage("修改成功");
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void onAlertPwdSuccess() {
        dialog.show();
    }

    @Override
    public void onAlertPwdError(String msg) {
        showToast(msg);
    }
}
